package net.suncaper.demo.controller;

import net.suncaper.demo.domain.*;
import net.suncaper.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/usermanager")
public class User_controller {
    private int select;
    private String UserId;
    @Autowired
    private UserService userService;
    @RequestMapping("/set")
    public String SetAdministratorId(@RequestParam(value = "UserId", required = false) String UserId,@RequestParam(value = "select", required = false) int select) {
        this.UserId=UserId;
        this.select=select;
        if(select==1){
            return "redirect:/graduate/set?UserId="+UserId+"&select=1";}
        if(select==2){
            return "redirect:/graduate/set?UserId="+UserId+"&select=2";}
        else{
            return "redirect:/graduate/set?UserId="+UserId+"&select=3";}
    }
    @RequestMapping("/studentadd")
    public String studentadd(){
        return "pages/user/StudentUserAdd.html";
    }
    @PostMapping("/studentadd")
    public String savestudent(Student student){
        userService.savestudent(student);
        return "redirect:/information";
    }
    @RequestMapping("/teacheradd")
    public String teacheradd(){
        return "pages/user/TeacherUserAdd.html";
    }
    @PostMapping("/teacheradd")
    public String saveteacher(Teacher teacher){
        userService.saveteacher(teacher);
        return "redirect:/information";
    }
    @RequestMapping("/studentdelete")
    public String deleteStudent(Model model,@RequestParam(value = "name", required = false) String studentId) {
        userService.deleteStudentById(studentId);
        return "redirect:/usermanager/studentmanager";
    }
    @RequestMapping("/studentupdate")
    public String studentupdate(Model model, @RequestParam(value = "name", required = false) String studentId) {
        Student student = userService.findStudentById(studentId);
        model.addAttribute("student", student);
        return "/pages/user/StudentUserUpdate.html";
    }
    @PostMapping("/studentupdate")
    public String UPdateStudent(Student student) {
        userService.updateStudent(student);
        return "redirect:/usermanager/studentmanager";
    }
    @RequestMapping("/studentmanager")
    public String studentmanager(Model model){
        List<Student> student = userService.findStudent();
        model.addAttribute("student",student);
        return "pages/user/StudentUserManager.html";
    }





    @RequestMapping("/teacherdelete")
    public String deleteTeacher(Model model,@RequestParam(value = "name", required = false) String teacherId) {
        userService.deleteTeacherById(teacherId);
        return "redirect:/usermanager/teachermanager";
    }
    @RequestMapping("/teacherupdate")
    public String teacherupdate(Model model, @RequestParam(value = "name", required = false) String teacherId) {
        Teacher teacher = userService.findTeacherById(teacherId);
        model.addAttribute("teacher", teacher);
        return "/pages/user/TeacherUserUpdate.html";
    }
    @PostMapping("/teacherupdate")
    public String UPdateTeacher(Teacher teacher) {
        userService.updateTeacher(teacher);
        return "redirect:/usermanager/teachermanager";
    }
    @RequestMapping("/teachermanager")
    public String teachermanager(Model model){
        List<Teacher> teacher = userService.findTeacher();
        model.addAttribute("teacher",teacher);
        return "pages/user/TeacherUserManager.html";
    }
}
